package com.jiedaibao;

import org.apache.catalina.LifecycleException;
import org.apache.catalina.startup.Tomcat;
import org.springframework.web.context.support.GenericWebApplicationContext;

import javax.servlet.ServletException;

public class MyServletWebServerApplicationContext {

    private Tomcat tomcat;


    protected void finishRefresh() {
        startWebServer();
    }

    protected void onRefresh() {
        createWebServer();
    }

    private void createWebServer() {
        this.tomcat = new Tomcat();
        tomcat.setPort(8080);
        try {
            tomcat.addWebapp("/", "/data");
        } catch (ServletException e) {
            e.printStackTrace();
        }
    }

    private void startWebServer(){
        try {
            tomcat.start();
            tomcat.getServer().await();
        } catch (LifecycleException e) {
            e.printStackTrace();
        }
    }
}
